﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Modules.MENU.Components;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.MENU.Controls
{
    public partial class StaticMenu : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LiteralControl literal = new LiteralControl("");
            literal.Text += "<div class='MenuLink'>";

            IList<Item> allItems = Item.SelectCollectionAll(this.ModuleID);
            IList<Item> topItems = allItems.Where(x => x.ParentID == 0).OrderBy(x => x.SortOrder).ToList();

            foreach (Item topItem in topItems)
            {
                IList<Item> childItems = allItems.Where(x => x.ParentID == topItem.ID).OrderBy(x => x.SortOrder).ToList();
                if (childItems.Count > 0)
                {
                    literal.Text += string.Format("<div class='MenuLinkGroup'><a href='{0}'>{1}</a></div>", topItem.Url, topItem.Title);
                    foreach (Item childItem in childItems)
                    {
                        literal.Text += string.Format("<a class='MenuLinkItem' href='{0}' target='{2}'>{3} {1}</a>", childItem.Url, childItem.Title, childItem.Target, string.Format("<img src='{0}' border='0' />", ResolveClientUrl(childItem.ImageUrl)));
                    }
                }
                else
                {
                    literal.Text += string.Format("<a class='MenuLinkGroup' href='{0}' target='{2}'>{3} {1}</a>", topItem.Url, topItem.Title, topItem.Target, string.Format("<img src='{0}' border='0' />", ResolveClientUrl(topItem.ImageUrl)));
                }
            }
            literal.Text += "</div>";
            ContentPane.Controls.Add(literal);
        }
    }
}